package com.gulimall.cart.controller;

import com.guigui.common.constant.CartConstant;
import com.gulimall.cart.interceptor.CookieInterceptor;
import com.gulimall.cart.services.CartServices;
import com.gulimall.cart.vo.Cart;
import com.gulimall.cart.vo.CartItem;
import com.gulimall.cart.vo.UserKeyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartServices cartServices;

    @GetMapping("/success.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("num") Integer num,
                          Model model) {

        CartItem cartItem = cartServices.addCart(skuId, num);
        model.addAttribute("item", cartItem);
        return "success";
    }

    @GetMapping("/cartList.html")
    public String cartList(Model model) {
        Cart cart = getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    @GetMapping("/changeSkuNum")
    public String changeSkuNum(@RequestParam("skuId") Long skuId,
                                @RequestParam("num") Integer modifyNum) {
        cartServices.changeSkuNum(skuId, modifyNum);
        return "redirect://cart.gulimall.com/cartList.html";
    }

    @GetMapping("/removeProduct")
    public String deleteProduct(@RequestParam("skuId") Long skuId) {
        cartServices.deleteProduct(skuId);
        return "redirect://cart.gulimall.com/cartList.html";
    }


    @GetMapping("/changeItemCheck")
    public String changeItemCheck(@RequestParam("skuId") Long skuId) {
        cartServices.changeItemCheck(skuId);
        return "redirect://cart.gulimall.com/cartList.html";
    }

    @GetMapping("/getCartItems")
    @ResponseBody
    public List<CartItem> getCartItems() {
        UserKeyInfo userKeyInfo = CookieInterceptor.userKeyInfoThreadLocal.get();
        if (userKeyInfo.getUserId() != null) {
            String redisKey = CartConstant.USER_KEY_SAVE_PREFIX + userKeyInfo.getUserId();
            return cartServices.getCartItems(redisKey);
        }
        return new ArrayList<>();
    }

    private Cart getCart() {
        UserKeyInfo userKeyInfo = CookieInterceptor.userKeyInfoThreadLocal.get();
        List<String> redisKey = new ArrayList<>(2);
        if (userKeyInfo.getUserId() != null) {
            redisKey.add(CartConstant.USER_KEY_SAVE_PREFIX + userKeyInfo.getUserId());
        }
        if (userKeyInfo.getUserKey() != null) {
            redisKey.add(CartConstant.USER_KEY_SAVE_PREFIX + userKeyInfo.getUserKey());
        }
        return redisKey.size() == 1 ? cartServices.getSingleCart(redisKey.get(0)) : cartServices.getMultiCartAndMerge(redisKey);
    }
}
